﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using QLMB.Models;
using QLMB.ViewModels;
using System.Web.UI.WebControls;

namespace QLMB.Controllers
{
    public class EditTicketController : BaseController
    {
        public ActionResult Index()
        {
            string mave = "";
            return View(mave);
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "ChangeDate")]
        public ActionResult ChangeDate(string mave)
        {   
            VECHUYENBAY vechuyenbay = Repository.LayVeChuyenBay(mave);
            CT_VECHUYENBAY ctdi = vechuyenbay.CT_VECHUYENBAY.OrderBy(x => x.CHUYENBAY.NgayDi).First();
            string madbdi = ctdi.CHUYENBAY.DUONGBAY.MaDuongBay;
            
            int sl = (int)vechuyenbay.SoLuong;
            string madvdi = ctdi.MaLoaiDV;
            ViewBag.CheckPemission1 = Repository.KiemTraDoiNgayBay(madvdi);
            ViewBag.DSCBDi = Repository.LayDSCB(madbdi,madvdi,sl).OrderBy(x=>x.NgayDi).ToList();
            ViewBag.Check = 1;
            DateTime ngaydi = (DateTime)ctdi.CHUYENBAY.NgayDi;
            if (ngaydi < DateTime.Now)
                ViewBag.PhiDi = ViewBag.CheckPemission1.GiaTien1;
            else
                ViewBag.PhiDi = ViewBag.CheckPemission1.GiaTien2;

            if(vechuyenbay.LoaiVe == "Khu hoi")
            {
                ViewBag.Check = 2;
                CT_VECHUYENBAY ctve = vechuyenbay.CT_VECHUYENBAY.OrderBy(x => x.CHUYENBAY.NgayDi).Last();
                string madbve = ctve.CHUYENBAY.DUONGBAY.MaDuongBay;
                string madvve = ctve.MaLoaiDV;
                ViewBag.CheckPemission2 = Repository.KiemTraDoiNgayBay(madvve);
                ViewBag.DSCBVe = Repository.LayDSCB(madbve,madvve,sl).OrderBy(x => x.NgayDi).ToList();
                ngaydi = (DateTime)ctve.CHUYENBAY.NgayDi;
                if (ngaydi < DateTime.Now)
                    ViewBag.PhiVe = ViewBag.CheckPemission2.GiaTien1;
                else
                    ViewBag.PhiVe = ViewBag.CheckPemission2.GiaTien2;

            }
            ViewBag.MAVE = vechuyenbay.MaVe;
            return View();
        }
        [HttpPost]
        public ActionResult ChangeDateTicket(string mave, string MaCBDi, string MaCBVe)
        {
            Repository.CapNhatVe(mave,MaCBDi,MaCBVe);
            return View();
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "ChangeRoad")]
        public ActionResult ChangeRoad(string mave, string MaCB)
        {
            ViewBag.MaVe = mave;
            ViewBag.MaCB = MaCB;
            return View();
        }

        public PartialViewResult _ChiTietVe(string mave)
        {
            VECHUYENBAY ve = Repository.LayVeChuyenBay(mave);
            IEnumerable<CHUYENBAY> dscb = Repository.LayDSChuyenBay(mave);
            CT_VECHUYENBAY luotdi = ve.CT_VECHUYENBAY.OrderBy(x => x.CHUYENBAY.NgayDi).First();
            ViewBag.NgayDi = string.Format("{0: dd/MM/yyyy}", luotdi.CHUYENBAY.NgayDi);
            ViewBag.GioDi = string.Format("{0: hh:mm}", luotdi.CHUYENBAY.NgayDi);
            ViewBag.DiemDi = luotdi.CHUYENBAY.DUONGBAY.SANBAY.THANHPHO.TenTP;
            ViewBag.NgayDen = string.Format("{0: dd/MM/yyyy}", luotdi.CHUYENBAY.NgayDen);
            ViewBag.GioDen = string.Format("{0: hh:mm}", luotdi.CHUYENBAY.NgayDen);
            ViewBag.DiemDen = luotdi.CHUYENBAY.DUONGBAY.SANBAY1.THANHPHO.TenTP;
            ViewBag.TongTien = ve.TongTien;
            ViewBag.LoaiDVDi = luotdi.LOAIDICHVU.TenDichVu;
            ViewBag.Check = 1;
            if(ve.LoaiVe == "Khu hoi")
            {
                CT_VECHUYENBAY luotve = ve.CT_VECHUYENBAY.OrderBy(x => x.CHUYENBAY.NgayDi).Last();
                ViewBag.GioDi2 = string.Format("{0: hh:mm}", luotve.CHUYENBAY.NgayDi);
                ViewBag.GioDen2 = string.Format("{0: hh:mm}", luotve.CHUYENBAY.NgayDen);
                ViewBag.NgayDi2 = string.Format("{0: dd/MM/yyyy}", luotve.CHUYENBAY.NgayDi);
                ViewBag.NgayDen2 = string.Format("{0: dd/MM/yyyy}", luotve.CHUYENBAY.NgayDen);
                ViewBag.LoaiDVVe = luotve.LOAIDICHVU.TenDichVu;
                ViewBag.Check = 2;
            }
            ViewBag.VeCB = ve;
            ViewBag.DSCB = dscb;
            return PartialView();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult KiemTraVe(string mave)
        {
            VECHUYENBAY ve = Repository.LayVeChuyenBay(mave);
            if (ve == null)
                return null;
            var json = new {ttv = ve.MaTrangThaiVe};
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LayDSChuyenBay(string mave)
        {
            IEnumerable<CHUYENBAY> dscb = Repository.LayDSChuyenBay(mave);
            List<ListItem> list = new List<ListItem>();
            foreach (CHUYENBAY item in dscb)
            {
                string temp = item.DUONGBAY.SANBAY.THANHPHO.TenTP + " --> " + item.DUONGBAY.SANBAY1.THANHPHO.TenTP; 
                list.Add(new ListItem { Value = item.MaCB, Text =  temp});
            }

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        
    }
}